package com.wee.aircoach_user;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.bigtotoro.util.SharedPreferencesUtil;
import com.bigtotoro.widgets.AppProgressBar;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.wee.entity.MD5Util;
import com.wee.entity.User_detail;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

public class ProfilePurposeActivity extends Activity implements View.OnClickListener {
    private TextView text0, text1, text2, text3;
    User_detail data1;
    int  it,exact;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile_purpose);
        exact=  SharedPreferencesUtil.getInt(this, Constant.USERTIME);
        it=  SharedPreferencesUtil.getInt(ProfilePurposeActivity.this, Constant.USERID);
        initView();
        gethttp();
    }

    /** 解析字段 */
    public void gethttp() {

        String user_main=Constant.USER_DETAIL+ MD5Util.md5(exact)+"&id="+it+"";
        HttpUtils httpUtils = new HttpUtils();
        httpUtils.send(HttpRequest.HttpMethod.GET,user_main ,
                new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        String str = responseInfo.result;
                        Gson gs = new Gson();
                        JsonObject jo = gs.fromJson(str, JsonObject.class);
                        JsonElement je = jo.get("status");
                        String status = je.getAsString();
                        if ("ok".equals(status)) {
                            JsonObject object = jo.getAsJsonObject("data");
                            data1 = gs.fromJson(object, User_detail.class);
                            updateView(data1);
                        }
                    }
                    @Override
                    public void onFailure(HttpException e, String s) {
                        System.out.println(e.getMessage());
                    }
                });

    }
    private void updateView(User_detail data) {
        text0.setSelected(false);  text1.setSelected(false);  text2.setSelected(false);  text3.setSelected(false);
        if(data.getPurpose()!=null){
            if(data.getPurpose().equals("减脂")){
                text0.setSelected(true);
            }else if(data.getPurpose().equals("塑形")){
                text1.setSelected(true);
            }else if(data.getPurpose().equals("增加耐力")){
                text2.setSelected(true);
            }else if(data.getPurpose().equals("增强力量")){
                text3.setSelected(true);
            }
        }
    }

    private void initView() {
        findViewById(R.id.back).setOnClickListener(this);
        findViewById(R.id.btn_right).setOnClickListener(this);
        text0 = (TextView)findViewById(R.id.tab0);
        text1 = (TextView)findViewById(R.id.tab1);
        text2 = (TextView)findViewById(R.id.tab2);
        text3 = (TextView)findViewById(R.id.tab3);
        text0.setOnClickListener(this);
        text1.setOnClickListener(this);
        text2.setOnClickListener(this);
        text3.setOnClickListener(this);
    }
    private int tab;
    private String updatePurpose = "";
    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.back){
            finish();
        }else if(v.getId() == R.id.tab0){
            tab = 0;

            selectTab(true);
        }else if(v.getId() == R.id.tab1){
            tab = 1;
            selectTab(true);
        }else if(v.getId() == R.id.tab2){
            tab = 2;
            selectTab(true);
        }else if(v.getId() == R.id.tab3){
            tab = 3;
            selectTab(true);
        }else if(v.getId() == R.id.btn_right){
            updatePurpose();
        }
    }

    private void updatePurpose() {
        if(data1 == null){
            Toast.makeText(this, "未登录", Toast.LENGTH_SHORT).show();
            return;
        }
        if(updatePurpose.equals("")||updatePurpose.equals(data1.getPurpose())){
            Toast.makeText(ProfilePurposeActivity.this, "请选择新的目标", Toast.LENGTH_SHORT).show();
            return;
        }
      /*  RequestParams params = new RequestParams();
        params.addBodyParameter("token", "123");
        params.addBodyParameter("userId", ""+it );
        params.addBodyParameter("purpose", ""+updatePurpose);
        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.POST,
                Constant.POP,
                params,
                new RequestCallBack<String>() {
                    @Override
                    public void onStart() {

                    }
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {
                    }
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        String str = responseInfo.result;
                        System.out.print("BBBBBBBBBBBCCCC"+str);
                        JSONObject json;
                        try {
                            json = new JSONObject(str);
                            if(json.getString("status").equals("ok")){
                                Toast.makeText(ProfilePurposeActivity.this, "更新健身目标成功", Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        System.out.print("IIIIIIIIII"+msg);
                        Toast.makeText(ProfilePurposeActivity.this, "更新健身目标失败", Toast.LENGTH_SHORT).show();
                        AppProgressBar.closeProgressBar();
                    }
                });*/


        RequestParams params = new RequestParams();
        params.put("token", "" +MD5Util.md5(exact));
        params.put("userId", "" + it);
        params.put("purpose", "" + updatePurpose);
        params.put("_type", "json");
        AppProgressBar.checkAndCreateProgressBar(this);
        MyApplication.getInstance().getHttpClient().get(Constant.POP, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                try {
                    JSONObject json = new JSONObject(new String(bytes));
                    if (json.getString("status").equals("ok")) {
                        Toast.makeText(ProfilePurposeActivity.this, "更新健身目标成功", Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                AppProgressBar.closeProgressBar();
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                throwable.printStackTrace();
                Toast.makeText(ProfilePurposeActivity.this, "更新健身目标失败", Toast.LENGTH_SHORT).show();
                AppProgressBar.closeProgressBar();
            }
        });
    }

    private void selectTab(boolean toggle){
        text0.setSelected(false);
        text1.setSelected(false);
        text2.setSelected(false);
        text3.setSelected(false);
        if(tab==0){
            text0.setSelected(true);
            updatePurpose = text0.getText().toString();
        }else if(tab==1){
            text1.setSelected(true);
            updatePurpose = text1.getText().toString();
        }else if(tab==2){
            text2.setSelected(true);
            updatePurpose = text2.getText().toString();
        }else if(tab==3){
            text3.setSelected(true);
            updatePurpose = text3.getText().toString();
        }
    }
}
